#include "MessageItemWidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QDateTime>

MessageItemWidget::MessageItemWidget(const QString &level, const QString &message, QWidget *parent)
    : QWidget(parent)
{
    setupUI(level, message);
}

void MessageItemWidget::setupUI(const QString &level, const QString &message)
{
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);

    // === 外层 Layout ===
    QVBoxLayout *outerLayout = new QVBoxLayout(this);
    outerLayout->setContentsMargins(0, 0, 0, 0);
    outerLayout->setSpacing(0);

    // === 样式区域 ===
    QWidget *styledWrapper = new QWidget(this);
    styledWrapper->setObjectName("logMessageWrapper");
    styledWrapper->setStyleSheet(QString(
                                     "#logMessageWrapper {"
                                     " background-color: rgba(255, 255, 255, 0.03);"
                                     " border-left: 4px solid %1;"
                                     " border-radius: 6px;"
                                     " padding: 12px;"
                                     "}").arg(levelToColor(level)));

    styledWrapper->setMinimumHeight(48);// 行为：控制最小高度

    QHBoxLayout *mainLayout = new QHBoxLayout(styledWrapper);
    mainLayout->setContentsMargins(20, 6, 20, 6);
    mainLayout->setSpacing(20);  // 稍微宽松

    // === 图标 ===
    iconLabel = new QLabel(styledWrapper);
    iconLabel->setFixedSize(20, 20);
    QPixmap iconPixmap(levelToIconPath(level));
    if (!iconPixmap.isNull()) {
        iconLabel->setPixmap(iconPixmap.scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    mainLayout->addWidget(iconLabel, 0, Qt::AlignVCenter);

    // === 时间 ===
    QLabel *timeLabel = new QLabel(QDateTime::currentDateTime().toString("HH:mm:ss"), styledWrapper);
    timeLabel->setStyleSheet("color: #94a3b8; font-size: 13px;background-color: transparent;");
    mainLayout->addWidget(timeLabel, 0, Qt::AlignVCenter);

    // === 消息内容 ===
    QString fullText = QString("[%1] %2").arg(level, message);
    textLabel = new QLabel(fullText, styledWrapper);
    textLabel->setStyleSheet("color: white; font-size: 14px;background-color: transparent;");
    textLabel->setTextFormat(Qt::PlainText);
    textLabel->setWordWrap(true);
    textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    mainLayout->addWidget(textLabel, 1, Qt::AlignVCenter);

    // === 加入外层 ===
    outerLayout->addWidget(styledWrapper);
}


QString MessageItemWidget::levelToColor(const QString &level)
{
    if (level == "Alarm") return "#60A5FA";     // 蓝
    if (level == "Warning") return "#4ADE80";   // 橙
    if (level == "Error") return "#FACC15";     // 红
    return "#4B5563";                           // 默认灰
}

QString MessageItemWidget::levelToIconPath(const QString &level)
{
    if (level == "Alarm") return ":/img/success.png";
    if (level == "Warning") return ":/img/warning.png";
    if (level == "Error") return ":/img/error.png";
    return ":/img/fa-cog.png";
}

QString MessageItemWidget::getText() {
    return textLabel ? textLabel->text() : "";
}

